import { AdManager } from "../ads/AdManager";
import { Tools } from "../common/Tools";
import { AudioManager } from "../manager/AudioManager";
import { UIManager } from "../manager/UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SettingUI1 extends cc.Component {

    @property(cc.Node)
    musicBtn: cc.Node = null;
    @property(cc.Node)
    effectBtn: cc.Node = null;
    @property(cc.Node)
    vibBtn: cc.Node = null;
    @property(cc.Node)
    subtitleBtn: cc.Node = null;
    @property(cc.Node)
    dialogueBtn: cc.Node = null;

    @property({ type: cc.Node, displayName: "关闭按钮" })
    closeBtn: cc.Node = null;


    @property(cc.Node)
    WinReGameBtn: cc.Node = null;

    @property(cc.Node)
    TexiaoBtn: cc.Node = null;
    @property(cc.Node)
    liveLevelBtn: cc.Node = null;
    @property(cc.Node)
    autoAnswerBtn: cc.Node = null;
    @property(cc.Node)
    showLvNameBtn: cc.Node = null;
    @property(cc.Node)
    kbTipBtn: cc.Node = null;
    @property(cc.Node)
    answerRightBtn: cc.Node = null;

    static isWinReGameBtn: boolean = false;

    static isCliCkTexiaoBtn: boolean = false;

    static isLiveLevelBtn: boolean = false;

    static isAutoAnswerBtn: boolean = false;

    static isShowLvNameBtn: boolean = false;

    static isKbTipBtn: boolean = false;

    static isAnswerRightBtn: boolean = false;


    start() {
        this.closeBtn.on("click", () => {
            AudioManager.playClick();
            UIManager.ins.close();
        });

        this.updateMusicBtn();
        this.updateEffectBtn();
        this.updateVibBtn();
        this.updateSubtitleBtn();
        this.updateDialogueBtn();


        let isWinReGameBtnBendi = Tools.getStorage('isWinReGameBtn')
        console.log("isWinReGameBtnBendi", isWinReGameBtnBendi);

        if (isWinReGameBtnBendi) {
            this.WinReGameBtn.getComponent(cc.Toggle).check()
            SettingUI1.isWinReGameBtn = true;
        }


        AdManager.isWinReGameBtn((active: boolean) => {
            this.WinReGameBtn.active = active;
        });

        let isCliCkTexiaoBtnBendi = Tools.getStorage('isCliCkTexiaoBtn')
        console.log("isCliCkTexiaoBtnBendi", isCliCkTexiaoBtnBendi);

        if (isCliCkTexiaoBtnBendi != "false") {
            this.TexiaoBtn.getComponent(cc.Toggle).check()
            SettingUI1.isCliCkTexiaoBtn = true;
        }

        AdManager.isLiveLevelBtn((active: boolean) => {
            this.liveLevelBtn.active = active;
            this.autoAnswerBtn.active = false;
            this.showLvNameBtn.active = active;
            this.kbTipBtn.active = false;
            this.answerRightBtn.active = false;
        });


        let isLiveLevelActive = Tools.getStorage('setLiveLvActive')
        if (isLiveLevelActive != "false") {
            this.liveLevelBtn.getComponent(cc.Toggle).check();
            SettingUI1.isLiveLevelBtn = true;
        } else {
            this.liveLevelBtn.getComponent(cc.Toggle).uncheck();
            SettingUI1.isLiveLevelBtn = false;
        }

        let isAutoAnswerActive = Tools.getStorage('setAutoAnswerActive')
        if (isAutoAnswerActive && isAutoAnswerActive != "false") {
            this.autoAnswerBtn.getComponent(cc.Toggle).check();
            SettingUI1.isAutoAnswerBtn = true;
        } else {
            this.autoAnswerBtn.getComponent(cc.Toggle).uncheck();
            SettingUI1.isAutoAnswerBtn = false;
        }

        let isShowLvNameActive = Tools.getStorage('setShowLvNameActive')
        if (isShowLvNameActive && isShowLvNameActive != "false") {
            this.showLvNameBtn.getComponent(cc.Toggle).check();
            SettingUI1.isShowLvNameBtn = true;
        } else {
            this.showLvNameBtn.getComponent(cc.Toggle).uncheck();
            SettingUI1.isShowLvNameBtn = false;
        }

        let isKbTipActive = Tools.getStorage('setKbTipActive')
        if (isKbTipActive && isKbTipActive != "false") {
            this.kbTipBtn.getComponent(cc.Toggle).check();
            SettingUI1.isKbTipBtn = true;
        } else {
            this.kbTipBtn.getComponent(cc.Toggle).uncheck();
            SettingUI1.isKbTipBtn = false;
        }

        let isAnswerRightActive = Tools.getStorage('setAnswerRightActive')
        if (isAnswerRightActive && isAnswerRightActive != "false") {
            this.answerRightBtn.getComponent(cc.Toggle).check();
            SettingUI1.isAnswerRightBtn = true;
        } else {
            this.answerRightBtn.getComponent(cc.Toggle).uncheck();
            SettingUI1.isAnswerRightBtn = false;
        }

    }
    updateMusicBtn() {
        this.musicBtn.getChildByName('open').active = AudioManager.isPlayBG;
        this.musicBtn.getChildByName('close').active = !AudioManager.isPlayBG;
    }
    updateEffectBtn() {
        this.effectBtn.getChildByName('open').active = AudioManager.isPlayAudio;
        this.effectBtn.getChildByName('close').active = !AudioManager.isPlayAudio;
    }
    updateVibBtn() {
        this.vibBtn.getChildByName('open').active = AudioManager.isVib;
        this.vibBtn.getChildByName('close').active = !AudioManager.isVib;
    }
    updateSubtitleBtn() {
        this.subtitleBtn.getChildByName('open').active = AudioManager.isSubtitle;
        this.subtitleBtn.getChildByName('close').active = !AudioManager.isSubtitle;
    }
    updateDialogueBtn() {
        this.dialogueBtn.getChildByName('open').active = AudioManager.isDialogue;
        this.dialogueBtn.getChildByName('close').active = !AudioManager.isDialogue;
    }

    clickWinReGameBtn() {
        SettingUI1.isWinReGameBtn = !SettingUI1.isWinReGameBtn
        console.log("isWinReGameBtn", SettingUI1.isWinReGameBtn);
        if (SettingUI1.isWinReGameBtn) {
            Tools.setStorage('isWinReGameBtn', SettingUI1.isWinReGameBtn)
        } else {
            Tools.setStorage('isWinReGameBtn', null)
        }
    }

    clickTexiaoBtn() {
        SettingUI1.isCliCkTexiaoBtn = !SettingUI1.isCliCkTexiaoBtn
        console.log("isCliCkTexiaoBtn", SettingUI1.isCliCkTexiaoBtn);
        if (SettingUI1.isCliCkTexiaoBtn) {
            Tools.setStorage('isCliCkTexiaoBtn', SettingUI1.isCliCkTexiaoBtn)
        } else {
            Tools.setStorage('isCliCkTexiaoBtn', "false")
        }
    }

    clickLiveLvBtn() {
        SettingUI1.isLiveLevelBtn = !SettingUI1.isLiveLevelBtn;
        Tools.setStorage('setLiveLvActive', SettingUI1.isLiveLevelBtn);
    }

    clickAutoAnswerBtn() {
        SettingUI1.isAutoAnswerBtn = !SettingUI1.isAutoAnswerBtn;
        Tools.setStorage('setAutoAnswerActive', SettingUI1.isAutoAnswerBtn);
    }

    clickShowLvNameBtn() {
        SettingUI1.isShowLvNameBtn = !SettingUI1.isShowLvNameBtn;
        Tools.setStorage('setShowLvNameActive', SettingUI1.isShowLvNameBtn);
    }

    clickKbTipBtn() {
        SettingUI1.isKbTipBtn = !SettingUI1.isKbTipBtn;
        Tools.setStorage('setKbTipActive', SettingUI1.isKbTipBtn);
    }

    clickARBtn() {
        SettingUI1.isAnswerRightBtn = !SettingUI1.isAnswerRightBtn;
        Tools.setStorage('setAnswerRightActive', SettingUI1.isAnswerRightBtn);
    }

    clickBtnMusic() {
        AudioManager.isPlayBG = !AudioManager.isPlayBG;
        AudioManager.setMusicStorage(AudioManager.isPlayBG);

        if (AudioManager.isPlayBG) {
            AudioManager.playBG("startBgm", 1);
        } else {
            AudioManager.stopBG();
        }

        this.updateMusicBtn();
    }
    clickBtnEffect() {
        AudioManager.isPlayAudio = !AudioManager.isPlayAudio;
        AudioManager.setAudioStorage(AudioManager.isPlayAudio);
        this.updateEffectBtn();
        AudioManager.playClick();
    }
    clickBtnVib() {
        AudioManager.isVib = !AudioManager.isVib;
        AudioManager.setVibStorage(AudioManager.isVib);
        this.updateVibBtn();
    }
    clickBtnSubtitle() {
        AudioManager.isSubtitle = !AudioManager.isSubtitle;
        AudioManager.setSubtitleStorage(AudioManager.isSubtitle);
        this.updateSubtitleBtn();
    }
    clickBtnDialogue() {
        AudioManager.isDialogue = !AudioManager.isDialogue;
        AudioManager.setDialogueStorage(AudioManager.isDialogue);
        this.updateDialogueBtn();
    }
}